<template>
  <el-dialog :visible="showRoleDialog" title="分配角色" @close="btnCancel">

    <el-checkbox-group v-model="roleIDs">
      <!-- 循环的选项 label 表示要存储的值 -->
      <el-checkbox v-for="item in list " :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="assignRoles">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDataById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      // 用户的ID 当前要处理哪个用户
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 承接角色列表
      roleIDs: [] // 这个数组用户存储当前用户的角色ID
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    async getRoles() {
      const { rows } = await getRoles({ page: 1, pagesize: 20 })
      this.list = rows
    },
    async getUserDataById(id) {
      const { roleIds } = await getUserDataById(id)
      // console.log(roleIds)
      this.roleIDs = roleIds
    },
    async assignRoles() {
      await assignRoles({ id: this.userId, roleIds: this.roleIDs })
      this.$emit('update:showRoleDialog', false)
    },
    btnCancel() {
      this.roleIDs = [] // 重置数组
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
